Home

Computer science

'

Practice Question  
Solved : 3

I need code for part 3. ( in java) please organize the code for the 3 classes after doing part 3.

// RubikRunner.java\r\nimport java.util.Scanner;\r\npublic class RubikRunner {\r\n   private RubikCube cube;\r\n   private Scanner scanner;\r\n public static void main(String[] args) {\r\n   RubikCube rc = new RubikCube();\r\n   System.out.println(rc);\r\n   Scanner scanner = new Scanner(System.in);\r\n   while (true) {\r\n     // Part 2: Add command loop for rotating the cube and exiting the program\r\n     System.out.println("What direction? q to quit");\r\n     System.out.print("What direction [right, left, up, down]: ");\r\n     String direction = scanner.nextLine().trim().toLowerCase();\r\n     if (direction.equals("q")) {\r\n       break;\r\n     } else {\r\n       // Call the corresponding rotation methods in RubikCube for the entered direction\r\n       if (direction.equals("right")) {\r\n         rc.right();\r\n       } else if (direction.equals("left")) {\r\n         rc.left();\r\n       } else if (direction.equals("up")) {\r\n         rc.up();\r\n       } else if (direction.equals("down")) {\r\n         rc.down();\r\n       } else {\r\n         System.out.println("Invalid direction.");\r\n       }\r\n       System.out.println(rc);\r\n     }\r\n   }\r\n }\r\n}
// RubikCube.java\r\nimport java.util.Arrays;\r\npublic class RubikCube {\r\n private RubikFace[] faces = new RubikFace[6];\r\n public RubikCube() {\r\n   // Part 1: Implement the RubikCube constructor\r\n   String[] colors = {\r\n     "White",\r\n     "Orange",\r\n     "Blue",\r\n     "Yellow",\r\n     "Red",\r\n     "Green"\r\n   };\r\n   for (int i = 0; i < colors.length; i++) {\r\n     faces[i] = new RubikFace(colors[i]);\r\n   }\r\n }\r\n   public String toString() {\r\n       // Part 1: Implement the toString() method\r\n       String result = "";\r\n       result += "  " + faces[0].toString() + "\\n"; \r\n       result += faces[2].toString() + " " + faces[1].toString() + " " + faces[5].toString() + "\\n";\r\n       result += "  " + faces[3].toString() + "\\n";\r\n       result += "  " + faces[4].toString() + "\\n";\r\n       return result;\r\n   }\r\n\r\n public void left() {\r\n   // Part 2: Implement the left() method\r\n   RubikFace temp = faces[0];\r\n   faces[0] = faces[3];\r\n   faces[3] = faces[5];\r\n   faces[5] = faces[1];\r\n   faces[1] = temp;\r\n }\r\n public void right() {\r\n   // Part 2: Implement the right() method\r\n   RubikFace temp = faces[0];\r\n   faces[0] = faces[1];\r\n   faces[1] = faces[5];\r\n   faces[5] = faces[3];\r\n   faces[3] = temp;\r\n }\r\n public void up() {\r\n   // Part 2: Implement the up() method\r\n   RubikFace temp = faces[0];\r\n   faces[0] = faces[2];\r\n   faces[2] = faces[5];\r\n   faces[5] = faces[4];\r\n   faces[4] = temp;\r\n }\r\n public void down() {\r\n   // Part 2: Implement the down() method\r\n   RubikFace temp = faces[0];\r\n   faces[0] = faces[4];\r\n   faces[4] = faces[5];\r\n   faces[5] = faces[2];\r\n   faces[2] = temp;\r\n }\r\n}
public class RubikFace\r\n{\r\n   private String color;\r\n   \r\n   public RubikFace(String _color)\r\n   {\r\n       color = _color;\r\n   }\r\n   public String toString()\r\n   {\r\n       return color.substring(0, 1);\r\n   }\r\n}

Part 3 - Nine-facet version of each face

In this part, you will be working mostly with the RubikFace class.  

Your assignment is to:

1) RubikFace - Add a 3x3 array to RubikFace that will hold the colors for each of the 9 facets on the face. For now, set each of the facets to be the same color as the face. You will also have to modify the toString method to return a 3x3 grid of colors. If, for example, the color of the face was Red, your toString method should return:

R R R
R R R
R R R

2) Update any methods in RubikRunner or RubikCube that require updating as a result of this change.

Starting with this activity, you need to comment on your code.

Sample Run

   W W W
      W W W
      W W W
B B B O O O G G G
B B B O O O G G G
B B B O O O G G G
      Y Y Y
      Y Y Y
      Y Y Y
      R R R
      R R R
      R R R

What direction? q to quit
What direction [right, left, up, down]: left
      W W W
      W W W
      W W W
O O O G G G R R R
O O O G G G R R R
O O O G G G R R R
      Y Y Y
      Y Y Y
      Y Y Y
      B B B
      B B B
      B B B

What direction? q to quit
What direction [right, left, up, down]: up
      G G G
      G G G
      G G G
O O O Y Y Y R R R
O O O Y Y Y R R R
O O O Y Y Y R R R
      B B B
      B B B
      B B B
      W W W
      W W W
      W W W

What direction? q to quit
What direction [right, left, up, down]: down
      W W W
      W W W
      W W W
O O O G G G R R R
O O O G G G R R R
O O O G G G R R R
      Y Y Y
      Y Y Y
      Y Y Y
      B B B
      B B B
      B B B

What direction? q to quit
What direction [right, left, up, down]: right
      W W W
      W W W
      W W W
B B B O O O G G G
B B B O O O G G G
B B B O O O G G G
      Y Y Y
      Y Y Y
      Y Y Y
      R R R
      R R R
      R R Rjava 

'

Answer